package com.ys.app_19;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.ys.http.download.DownloadTask;
import com.ys.http.download.OnDownloadListener;
import com.ys.http.entry.Resp;
import com.ys.http.network.Callback;
import com.ys.http.network.Header;
import com.ys.http.network.HttpUtils;
import com.ys.http.network.Params;
import com.ys.http.network.RespCallback;
import com.ys.http.network.SchedulerProvider;
import com.ys.http.network.Type;
import com.ys.http.upload.OnUploadListener;
import com.ys.http.utils.ByteUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import io.reactivex.annotations.NonNull;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_get).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //httpGet();
                //task.stop();
                //httpDownFile();
                //httpDownFil2e();


                File file = new File("sdcard/yuan.mp4");
                String uploadUrl = "https://clouddisk.esoncloud.com/clouddisk/api/device/uploadFile";
                String token = "eyJhbGciOiJIUzUxMiJ9.eyJyYW5kb21LZXkiOiI4OGxzOG4iLCJzdWIiOiIzOCIsImV4cCI6MTY1MjM0Nzk3MiwiaWF0IjoxNjUyMjYxMjcyfQ.GLnQ8adThfSkTFmkvQrGHpsQT_KnjEOzLiM5hVM5SlQ3Xhcj8XBPxonyjENkqkG5zRXsZJAyaLW0cdRqQL3W0Q";

                Params params = Params.newBuilder()
                        .add("parentFolderId", -1)
                        .add("fileName", file.getName())
                        .add("teamId", (String)null)
                        .build();
                Header header = Header.newBuilder()
                        .add("token","Bearer "+ token)
                        .build();
                HttpUtils.uploadFile(uploadUrl, file, params, header, new OnUploadListener() {
                    @Override
                    public void onProgress(int progress, long block) {
                    }

                    @Override
                    public void onResp(Resp<String> resp) {
                    }

                    @Override
                    public void onFailure(Throwable e) {
                    }
                });
            }
        });

        findViewById(R.id.btn_post).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                test();
                if (true) {
                    return;
                }

                //httpPost();
                //updateFileToWeb();
                //httpDownFil2e();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                options.inDensity = 0;
                options.inScreenDensity = 0;
                options.inTargetDensity = 0;
                BitmapFactory.decodeFile("sdcard/test-bbb.jpg", options);
                System.out.println(options.outWidth + " | " + options.outHeight);

                options.inSampleSize = 2;
                options.inJustDecodeBounds = false;
                ImageView imageView = findViewById(R.id.iv_test);
                imageView.setImageBitmap(BitmapFactory.decodeFile("sdcard/666.jpg"));
                System.out.println(options.outWidth + " | " + options.outHeight);
                readPictureDegree("sdcard/666.jpg");
                /*Glide.with(v.getContext()).clear(imageView);
                FutureTarget<Bitmap> target = Glide.with(v.getContext())
                        .asBitmap()
                        .load("sdcard/test-bbb.jpg")
                        .override(720)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .submit();
                try {
                    new Thread(()->{
                        Bitmap bitmap = null;
                        try {
                            bitmap = target.get();
                            System.out.println(bitmap.getWidth() + " | " + bitmap.getHeight());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }).start();

                    //imageView.setImageBitmap(bitmap);
                } catch (Exception e) {
                    e.printStackTrace();
                }*/
                //.load("http://47.107.50.81:8085//upload/visitorImage/202207010141911e192ff402f8e44269beb.jpg")
                //.into(imageView);
            }
        });

/*        Object obj = JsonUtils.newBuilder()
                .add("sn", "301F9A6063E9")
                .build();
        String url = "http://meeting.esoncloud.com/mtapi/mtgRoom/mtgRoomList";
        HttpUtils.postJson(url, obj, new RespCallback<RespRoom>() {
            @Override
            public void onError(@NonNull Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onSuccess(RespRoom s) {
                //System.out.println(s.list.get(0).getRoomName());
            }
        });*/


        //httpDownFile();
        //test();
    }


    //获取图片旋转角度
    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("aaaaaaa==> " + degree);
        return degree;
    }


    private void test() {
        //entity.setRoomName("dd");
        String url = "http://www.weather.com.cn/data/sk/101010100.html";
        HttpUtils.get(url, String.class)
                .compose(SchedulerProvider.applyScheduler())
                .subscribe(new Callback<String>() {
                    @Override
                    public void onSuccess(String s) {
                        System.out.println("---class-->" + s);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                    }
                });

        HttpUtils.get(url, new Type<String>(){})
                .compose(SchedulerProvider.applyScheduler())
                .subscribe(new Callback<String>() {
                    @Override
                    public void onSuccess(String s) {
                        System.out.println("----->" + s);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                    }
                });
    }

    private void updateFileToWeb() {
        Header header = Header.newBuilder().build();
        File file = new File("");
        HttpUtils.uploadFile("", header, file, new OnUploadListener() {
            @Override
            public void onProgress(int progress, long block) {

            }

            @Override
            public void onResp(Resp<String> resp) {

            }

            @Override
            public void onFailure(Throwable e) {

            }
        });
    }

    private void httpPost() {
        Header header = Header.newBuilder()
                .build();
        HttpUtils.post("", header, new RespCallback<String>() {
            @Override
            public void onSuccess(String s) {

            }

            @Override
            public void onError(@NonNull Throwable e) {

            }
        });

        Object obj = null;
        RequestBody body = null;

        HttpUtils.post("d", body, new Callback<String>() {
            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onSuccess(String s) {

            }
        });
    }

    private void httpGet() {
        HttpUtils.get("", new Callback<String>() {
            @Override
            public void onSuccess(String s) {

            }

            @Override
            public void onError(@NonNull Throwable e) {

            }
        });

        HttpUtils.get("url", new Callback<String>() {
            @Override
            public void onSuccess(String s) {

            }

            @Override
            public void onError(@NonNull Throwable e) {

            }
        });


        /***
         * 这里处理过code msg
         */
        HttpUtils.get("url", new RespCallback<String>() {
            @Override
            public void onSuccess(String s) {

            }

            @Override
            public void onError(@NonNull Throwable e) {

            }
        });

        HttpUtils.post("url", new Callback<String>() {
            @Override
            public void onSuccess(String s) {

            }
            @Override
            public void onError(@NonNull Throwable e) {

            }
        });

    }

    DownloadTask task;

    private void httpDownFile() {
        String url = "http://cps.yingyonghui.com/cps/yyh/channel/ac.union.m2/com.yingyonghui.market_1_30064511.apk";
        //String aaa = "https://down.qq.com/qqweb/PCQQ/PCQQ_EXE/PCQQ2021.exe";
        task = new DownloadTask.Builder()
                .setDownloadUrl(url)
                .setSavePath("/sdcard/test.exe")
                .setOnDownloadListener(new OnDownloadListener() {

                    @Override
                    public void onProgress(int progress, long speed) {
                        System.out.println(progress + "/" + speed + " ------> " + ByteUtils.getFormatByte(speed) + "/s");
                    }

                    @Override
                    public void onSuccess(String filePath) {

                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }
                })
                .build();
        task.start();
    }

    private void httpDownFil2e() {
        String url = "http://47.107.50.81:8085//upload/visitorImage/202207010141911e192ff402f8e44269beb.jpg";
       /* HttpUtils.downloadFile("/sdcard/202207010141911e192ff402f8e44269beb.jpg", url, new OnDownloadListener() {
            @Override
            public void onProgress(int progress, long speed) {
                System.out.println(progress + "/" + speed + " ------> " + ByteUtils.getFormatByte(speed) + "/s");
            }

            @Override
            public void onSuccess(String filePath) {

            }

            @Override
            public void onFailure(Throwable e) {
                e.printStackTrace();
            }
        });*/

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        client.newCall(request)
                .enqueue(new okhttp3.Callback() {
                    @Override
                    public void onFailure(@androidx.annotation.NonNull Call call, @androidx.annotation.NonNull IOException e) {

                    }

                    @Override
                    public void onResponse(@androidx.annotation.NonNull Call call, @androidx.annotation.NonNull Response response) throws IOException {
                        File file = new File("/sdcard/test-aaa.jpg");
                        /*BufferedSource is = response.body().source();
                        FileOutputStream fos = new FileOutputStream(file);
                        byte[] size = new byte[8192];
                        int len;
                        while ((len = is.read(size)) != -1) {
                            fos.write(size, 0, len);
                        }
                        fos.flush();
                        fos.close();*/

                        byte[] bytes = response.body().bytes();
                        FileOutputStream fos = new FileOutputStream(file);
                        fos.write(bytes);
                        fos.flush();
                        fos.close();
                    }
                });

    }
}